package program;

import java.io.File;

import kernel.FileManager;
import kernel.exception.OSException;
import kernel.process.AbstractProcess;
import kernel.process.AbstractProcessHeader;

/**
 * Touch process.
 * For more see man() method.
 * @author Petr Hajek, Veronika Vencurikova
 *
 */
public class Touch extends AbstractProcess {

	public Touch(AbstractProcessHeader header, long pid) {
		super(header, pid);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String continueJob() throws OSException {
		this.shouldEnd = true;
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String finalizeJob() throws OSException {
		return null;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String doJob(String input) throws OSException {
		if (this.getParams().size() < 1) throw new OSException(OSException.NO_ARGS); 
		else if (this.getParams().size() > 1) throw new OSException(OSException.ERR_MANY_ARGS); 
		
		File path = absPath(this.params.get(0), shellID);
		if (FileManager.exists(path) && !FileManager.existsDir(path)) FileManager.actualizeTimestamp(path); 
		else if (FileManager.existsDir(path)) throw new OSException(OSException.ERR_NOT_FILE); 
		else FileManager.createFile(path);
		return "";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean needParams() {
		return true;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void kill() throws OSException {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String man(String command) {
	String man = "";
		
		man += "    creates a new file, in case the file exists, changes time of the last modification\n";
		man += "\n";
		man += "Command syntax:\n";
		man += "    touch [FILE]\n";
		
		return man;
	}

}
